<template>
  <el-dialog
    :title="form.id ? '编辑' : '林权流转台账登记'"
    v-model="visible"
    :close-on-click-modal="false"
    draggable
  >
    <el-form
      ref="dataFormRef"
      :model="form"
      :rules="dataRules"
      formDialogRef
      v-loading="loading"
      label-width="120"
    >
      <!-- label-position="left" -->
      <el-row :gutter="24">
        <el-col :span="12" class="mb20">
          <el-form-item label="台账编号" prop="tzbh">
            <el-input v-model="form.tzbh" placeholder="请输入台账编号" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="登记日期" prop="djrq">
            <el-input v-model="form.djrq" placeholder="请输入登记日期" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="登记单位" prop="djdw">
            <el-input v-model="form.djdw" placeholder="请输入登记单位" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-tabs class="demo-tabs" type="border-card" v-model="activeName">
        <el-tab-pane label="基础信息" name="first">
          <template #label>
            <span class="label-title">基础信息</span>
          </template>
          <el-row :gutter="24">
            <el-col :span="24" class="mb20">
              <el-form-item label="流转合同编号" prop="lzhtbh">
                <el-input v-model="form.lzhtbh" placeholder="请输入流转合同编号" />
              </el-form-item>
            </el-col>
            <el-col :span="24" class="mb20">
              <el-form-item label="流转类型" prop="lclx">
                <el-radio-group v-model="form.lclx">
                  <el-radio
                    :label="item.value"
                    v-for="(item, index) in transfer_type"
                    :key="index"
                    >{{ item.label }}
                  </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="12" class="mb20" v-if="form.lclx == '9'">
              <el-form-item label="流转其他类型" prop="lcqtlx">
                <el-input v-model="form.lcqtlx" placeholder="请输入流转其他类型" />
              </el-form-item>
            </el-col>
            <el-col :span="24" class="mb20">
              <el-form-item label="流转方式" prop="lzfs">
                <el-radio-group v-model="form.lzfs">
                  <el-radio
                    :label="item.value"
                    v-for="(item, index) in transfer_way"
                    :key="index"
                    >{{ item.label }}
                  </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="12" class="mb20" v-if="form.lclx == '9'">
              <el-form-item label="流转其他方式" prop="lcqtfs">
                <el-input v-model="form.lcqtfs" placeholder="请输入流转其他方式" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
        <el-tab-pane label="权属主题信息" name="second">
          <template #label>
            <span class="label-title">权属主题信息</span>
          </template>
          <el-row :gutter="24">
            <el-col :span="12" class="mb20">
              <div slot="header" align="center"><span>出让方</span></div>
              <el-row :gutter="24">
                <el-col :span="24" class="mb20">
                  <el-form-item label="姓名" prop="crfXm">
                    <el-input v-model="form.crfXm" placeholder="请输入出让方姓名" />
                  </el-form-item>
                </el-col>
                <el-col :span="24" class="mb20">
                  <el-form-item label="单位" prop="crfDw">
                    <el-input v-model="form.crfDw" placeholder="请输入出让方单位" />
                  </el-form-item>
                </el-col>
                <el-col :span="24" class="mb20">
                  <el-form-item label="证件号码" prop="crfZjhm">
                    <el-input v-model="form.crfZjhm" placeholder="请输入出让方证件号码" />
                  </el-form-item>
                </el-col>
                <el-col :span="24" class="mb20">
                  <el-form-item label="联系方式" prop="crfLxfs">
                    <el-input v-model="form.crfLxfs" placeholder="请输入出让方联系方式" />
                  </el-form-item>
                </el-col>
                <el-col :span="24" class="mb20">
                  <el-form-item label="集体经济组织" prop="crfJtjjzz">
                    <el-input
                      v-model="form.crfJtjjzz"
                      placeholder="请输入出让方集体经济组织"
                    />
                  </el-form-item>
                </el-col>
              </el-row>
            </el-col>
            <!-- <div
              style="
                height: 100%;
                width: 1px;
                background: #ececec;
                position: relative;
                position: absolute;
                left: 50%;
              "
            ></div> -->
            <el-col :span="12" class="mb20">
              <div slot="header" align="center"><span>受让方</span></div>
              <el-row :gutter="24">
                <el-col :span="24" class="mb20">
                  <el-form-item label="姓名" prop="srfXm">
                    <el-input v-model="form.srfXm" placeholder="请输入受让方姓名" />
                  </el-form-item>
                </el-col>
                <el-col :span="24" class="mb20">
                  <el-form-item label="单位" prop="srfDw">
                    <el-input v-model="form.srfDw" placeholder="请输入受让方单位" />
                  </el-form-item>
                </el-col>
                <el-col :span="24" class="mb20">
                  <el-form-item label="证件号码" prop="srfZjhm">
                    <el-input v-model="form.srfZjhm" placeholder="请输入受让方证件号码" />
                  </el-form-item>
                </el-col>
                <el-col :span="24" class="mb20">
                  <el-form-item label="联系方式" prop="srfLxfs">
                    <el-input v-model="form.srfLxfs" placeholder="请输入受让方联系方式" />
                  </el-form-item>
                </el-col>
                <el-col :span="24" class="mb20">
                  <el-form-item label="集体经济组织" prop="srfJtjjzz">
                    <el-input
                      v-model="form.srfJtjjzz"
                      placeholder="请输入受让方集体经济组织"
                    />
                  </el-form-item>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </el-tab-pane>
        <el-tab-pane label="宗地信息" name="third">
          <template #label>
            <span class="label-title">宗地信息</span>
          </template>
          <el-row :gutter="24">
            <el-col :span="24" class="mb20">
              <el-form-item label="宗地编号" prop="zdbh">
                <el-input v-model="form.zdbh" placeholder="请输入宗地编号" />
              </el-form-item>
            </el-col>
            <el-col :span="24" class="mb20">
              <el-form-item label="坐落位置" prop="zlwz">
                <el-input v-model="form.zlwz" placeholder="请输入坐落位置" />
              </el-form-item>
            </el-col>
            <el-col :span="24" class="mb20">
              <el-form-item label="面积" prop="mj">
                <el-input v-model="form.mj" placeholder="请输入面积" />
              </el-form-item>
            </el-col>
            <el-col :span="24" class="mb20">
              <el-form-item label="林种" prop="lz">
                <el-radio-group v-model="form.lz">
                  <el-radio
                    :label="item.value"
                    v-for="(item, index) in forest_category"
                    :key="index"
                    >{{ item.label }}
                  </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="24" class="mb20">
              <el-form-item label="主要树种" prop="zysz">
                <el-radio-group v-model="form.zysz">
                  <el-radio
                    :label="item.value"
                    v-for="(item, index) in trees_varieties"
                    :key="index"
                    >{{ item.label }}
                  </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="24" class="mb20">
              <el-form-item label="林权证号" prop="lqzh">
                <el-input v-model="form.lqzh" placeholder="请输入林权证号" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
        <el-tab-pane label="流转条款" name="fourth">
          <template #label>
            <span class="label-title">流转条款</span>
          </template>
          <el-row :gutter="24">
            <el-col :span="24" class="mb20">
              <el-form-item label="流转期限" prop="lcqx">
                <el-input v-model="form.lcqx" placeholder="请输入流转期限" />
              </el-form-item>
            </el-col>
            <el-col :span="24" class="mb20">
              <el-form-item label="流转价格（元）" prop="lcjg">
                <el-input-number
                  :min="1"
                  :max="1000"
                  v-model="form.lcjg"
                  placeholder="请输入流转价格"
                ></el-input-number>
              </el-form-item>
            </el-col>
            <el-col :span="24" class="mb20">
              <el-form-item label="支付方式" prop="zffs">
                <el-radio-group v-model="form.zffs">
                  <el-radio
                    :label="item.value"
                    v-for="(item, index) in payment_way"
                    :key="index"
                    >{{ item.label }}
                  </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="24" class="mb20">
              <el-form-item label="备案情况" prop="baqk">
                <el-radio-group v-model="form.baqk">
                  <el-radio
                    :label="item.value"
                    v-for="(item, index) in record_state"
                    :key="index"
                    >{{ item.label }}
                  </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
        <el-tab-pane label="宗地信息" name="fifth">
          <template #label>
            <span class="label-title">宗地信息</span>
          </template>
          <el-row :gutter="24">
            <el-col :span="24" class="mb20">
              <el-form-item label="再流转限制" prop="zlzxq">
                <el-radio-group v-model="form.zlzxq">
                  <el-radio
                    :label="item.value"
                    v-for="(item, index) in transfer_limitation"
                    :key="index"
                  >
                    {{ item.label }}
                  </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="24" class="mb20">
              <el-form-item label="生态义务继承" prop="stywjc">
                <el-radio-group v-model="form.stywjc">
                  <el-radio
                    :label="item.value"
                    v-for="(item, index) in ecological_obligation_inheritance"
                    :key="index"
                    >{{ item.label }}
                  </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="24" class="mb20" v-if="form.stywjc == '9'">
              <el-form-item label="生态义务其他继承" prop="stywqtjc">
                <el-input v-model="form.stywqtjc" placeholder="请输入生态义务其他继承" />
              </el-form-item>
            </el-col>
            <el-col :span="24" class="mb20">
              <el-form-item label="争议解决方式" prop="zyjjfs">
                <el-radio-group v-model="form.zyjjfs">
                  <el-radio
                    :label="item.value"
                    v-for="(item, index) in dispute_tackle_way"
                    :key="index"
                  >
                    {{ item.label }}
                  </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>

      <el-row :gutter="24" v-if="false">
        <el-col :span="12" class="mb20">
          <el-form-item label="初审意见" prop="csYj">
            <el-input v-model="form.csYj" placeholder="请输入初审意见" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="初审日期" prop="csRq">
            <el-input v-model="form.csRq" placeholder="请输入初审日期" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="初审人员" prop="csRy">
            <el-input v-model="form.csRy" placeholder="请输入初审人员" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="初审备注" prop="csBz">
            <el-input v-model="form.csBz" placeholder="请输入初审备注" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="复核意见" prop="fhYj">
            <el-input v-model="form.fhYj" placeholder="请输入复核意见" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="复核日期" prop="fhRq">
            <el-input v-model="form.fhRq" placeholder="请输入复核日期" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="复核人员" prop="fhRy">
            <el-input v-model="form.fhRy" placeholder="请输入复核人员" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="复核备注" prop="fhBz">
            <el-input v-model="form.fhBz" placeholder="请输入复核备注" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="备案意见" prop="baYj">
            <el-input v-model="form.baYj" placeholder="请输入备案意见" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="备案日期" prop="baRq">
            <el-input v-model="form.baRq" placeholder="请输入备案日期" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="备案人员" prop="baRy">
            <el-input v-model="form.baRy" placeholder="请输入备案人员" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="备案备注" prop="baBz">
            <el-input v-model="form.baBz" placeholder="请输入备案备注" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit" :disabled="loading">确 认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="TransferForestBookRegisterDialog">
// ========== 1. 导入语句 ==========
import { useDict } from "/@/hooks/dict";
import { rule } from "/@/utils/validate";
import { useMessage } from "/@/hooks/message";
import {
  getObj,
  addObj,
  putObj,
  validateExist,
} from "/@/api/forestry/transferForestBookRegister";

// ========== 2. 组件定义 ==========
// 定义组件事件
const emit = defineEmits(["refresh"]);

// ========== 3. 响应式数据定义 ==========
// 基础响应式变量
const dataFormRef = ref(); // 表单引用
const visible = ref(false); // 弹窗显示状态
const loading = ref(false); // 加载状态
const activeName = ref("first");
// 表单数据对象
const form = reactive({
  id: "", // 主键
  tzbh: "", // 台账编号
  djrq: "", // 登记日期
  djdw: "", // 登记单位
  lzhtbh: "", // 流转合同编号
  lclx: "", // 流转类型
  lcqtlx: "", // 流转其他类型
  lzfs: "", // 流转方式
  lcqtfs: "", // 流转其他方式
  crfXm: "", // 出让方姓名
  crfDw: "", // 出让方单位
  crfZjhm: "", // 出让方证件号码
  crfLxfs: "", // 出让方联系方式
  crfJtjjzz: "", // 出让方集体经济组织
  srfXm: "", // 受让方姓名
  srfDw: "", // 受让方单位
  srfZjhm: "", // 受让方证件号码
  srfLxfs: "", // 受让方联系方式
  srfJtjjzz: "", // 受让方集体经济组织
  zdbh: "", // 宗地编号
  zlwz: "", // 坐落位置
  mj: "", // 面积
  lz: "", // 林种
  zysz: "", // 主要树种
  lqzh: "", // 林权证号
  lcqx: "", // 流转期限
  lcjg: 0, // 流转价格
  zffs: "", // 支付方式
  baqk: "", // 备案情况
  zlzxq: "", // 再流转限制
  stywjc: "", // 生态义务继承
  stywqtjc: "", // 生态义务其他继承
  zyjjfs: "", // 争议解决方式
  csYj: "", // 初审意见
  csRq: "", // 初审日期
  csRy: "", // 初审人员
  csBz: "", // 初审备注
  fhYj: "", // 复核意见
  fhRq: "", // 复核日期
  fhRy: "", // 复核人员
  fhBz: "", // 复核备注
  baYj: "", // 备案意见
  baRq: "", // 备案日期
  baRy: "", // 备案人员
  baBz: "", // 备案备注
});

// ========== 4. 字典数据处理 ==========
// 加载字典数据
const {
  forest_category,
  record_state,
  transfer_way,
  transfer_type,
  trees_varieties,
  payment_way,
  ecological_obligation_inheritance,
  dispute_tackle_way,
  transfer_limitation,
} = useDict(
  "forest_category",
  "record_state",
  "transfer_way",
  "transfer_type",
  "trees_varieties",
  "payment_way",
  "ecological_obligation_inheritance",
  "dispute_tackle_way",
  "transfer_limitation"
);

// ========== 5. 表单校验规则 ==========
const dataRules = ref({
  tzbh: [{ required: true, message: "台账编号不能为空", trigger: "blur" }],
  djrq: [{ required: true, message: "登记日期不能为空", trigger: "blur" }],
  djdw: [{ required: true, message: "登记单位不能为空", trigger: "blur" }],
  lzhtbh: [{ required: true, message: "流转合同编号不能为空", trigger: "blur" }],
  lclx: [{ required: true, message: "流转类型不能为空", trigger: "blur" }],
  lcqtlx: [{ required: true, message: "流转其他类型不能为空", trigger: "blur" }],
  lzfs: [{ required: true, message: "流转方式不能为空", trigger: "blur" }],
  crfXm: [{ required: true, message: "出让方姓名不能为空", trigger: "blur" }],
  crfDw: [{ required: true, message: "出让方单位不能为空", trigger: "blur" }],
  crfZjhm: [{ required: true, message: "出让方证件号码不能为空", trigger: "blur" }],
  crfLxfs: [{ required: true, message: "出让方联系方式不能为空", trigger: "blur" }],
  crfJtjjzz: [{ required: true, message: "出让方集体经济组织不能为空", trigger: "blur" }],
  srfXm: [{ required: true, message: "受让方姓名不能为空", trigger: "blur" }],
  srfDw: [{ required: true, message: "受让方单位不能为空", trigger: "blur" }],
  srfZjhm: [{ required: true, message: "受让方证件号码不能为空", trigger: "blur" }],
  srfLxfs: [{ required: true, message: "受让方联系方式不能为空", trigger: "blur" }],
  zdbh: [{ required: true, message: "宗地编号不能为空", trigger: "blur" }],
  zlwz: [{ required: true, message: "坐落位置不能为空", trigger: "blur" }],
  mj: [{ required: true, message: "面积不能为空", trigger: "blur" }],
  lz: [{ required: true, message: "林种不能为空", trigger: "blur" }],
  zysz: [{ required: true, message: "主要树种不能为空", trigger: "blur" }],
  lqzh: [{ required: true, message: "林权证号不能为空", trigger: "blur" }],
  lcqx: [{ required: true, message: "流转期限不能为空", trigger: "blur" }],
  lcjg: [{ required: true, message: "流转价格不能为空", trigger: "blur" }],
  zffs: [{ required: true, message: "支付方式不能为空", trigger: "blur" }],
  baqk: [{ required: true, message: "备案情况不能为空", trigger: "blur" }],
  zlzxq: [{ required: true, message: "再流转限制不能为空", trigger: "blur" }],
  stywjc: [{ required: true, message: "生态义务继承不能为空", trigger: "blur" }],
  zyjjfs: [{ required: true, message: "争议解决方式不能为空", trigger: "blur" }],
});

// ========== 6. 方法定义 ==========
// 获取详情数据
const getTransferForestBookRegisterData = async (id: string) => {
  try {
    loading.value = true;
    const { data } = await getObj({ id: id });
    // 直接将第一条数据赋值给表单
    Object.assign(form, data[0]);
  } catch (error) {
    useMessage().error("获取数据失败");
  } finally {
    loading.value = false;
  }
};

// 打开弹窗方法
const openDialog = (id: string) => {
  visible.value = true;
  form.id = "";

  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields();
  });

  // 获取TransferForestBookRegister信息
  if (id) {
    form.id = id;
    getTransferForestBookRegisterData(id);
  }
};

// 提交表单方法
const onSubmit = async () => {
  loading.value = true; // 防止重复提交

  // 表单校验
  const valid = await dataFormRef.value.validate().catch(() => {});
  if (!valid) {
    loading.value = false;
    useMessage().error("还有信息未填写完毕");
    return false;
  }

  try {
    // 根据是否有ID判断是新增还是修改
    form.id ? await putObj(form) : await addObj(form);
    useMessage().success(form.id ? "修改成功" : "添加成功");
    visible.value = false;
    emit("refresh"); // 通知父组件刷新列表
  } catch (err: any) {
    useMessage().error(err.msg);
  } finally {
    loading.value = false;
  }
};

// ========== 7. 对外暴露 ==========
// 暴露方法给父组件
defineExpose({
  openDialog,
});
</script>
<style scoped lang="scss">
.label-title::before {
  content: "*";
  color: var(--el-color-danger);
  margin-right: 4px;
}
</style>
